/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MostrarCurso.java
 *
 * Created on 08/08/2012, 05:32:05 PM
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.dataLayer.CarreraData;
import com.sistema.gestionuniversitaria.dataLayer.CursoData;
import com.sistema.gestionuniversitaria.dataLayer.ExcepcionGlobal;
import com.sistema.gestionuniversitaria.dataLayer.NoDataException;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Curso;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author maxwell
 */
public class MostrarCurso extends javax.swing.JInternalFrame {

    /**
     * Creates new form MostrarCurso
     */
    CursoData cuData = new CursoData();
    CarreraData caData = new CarreraData();
    DefaultTableModel modelo = new DefaultTableModel();

    public MostrarCurso() throws SQLException, ExcepcionGlobal, NoDataException {
        initComponents();
        jTbxNombre.setText("--Inhabilitado--");
        jTbxSigla.setText("--Inhabilitado--");
        jComboCarreras.removeAllItems();
        cargarListaCarreras();
        btnGruModoBusqueda.add(jRadioBCarrera);
        btnGruModoBusqueda.add(jRadioBSigla);
        btnGruModoBusqueda.add(jRadioBNombre);
    }

    private void llenarTabla() throws SQLException, ExcepcionGlobal, NoDataException {

        modelo = new DefaultTableModel();
        ArrayList<Curso> listaCursos = new ArrayList<Curso>();
        if (jRadioBCarrera.isSelected()) {
            Carrera carrSeleccionada = (Carrera) jComboCarreras.getSelectedItem();
            listaCursos = cuData.getCursosPorCarrera(carrSeleccionada.getCodigo());
        }
        if (jRadioBNombre.isSelected()) {
            listaCursos = cuData.getCursosPorNombre(jTbxNombre.getText().toString().toUpperCase());
        }
        if (jRadioBSigla.isSelected()) {
            Curso curso = cuData.getCursoPorSigla(jTbxSigla.getText().toString().toUpperCase());
            listaCursos.add(curso);
        }
        modelo.setColumnCount(0);
        modelo.addColumn("Sigla");
        modelo.addColumn("Nombre");
        modelo.addColumn("Créditos");
        modelo.setNumRows(listaCursos.size());
        int contador = 0;
        for (Curso curso : listaCursos) {
            modelo.setValueAt(curso.getSigla(), contador, 0);
            modelo.setValueAt(curso.getNombre(), contador, 1);
            modelo.setValueAt(curso.getCreditos(), contador, 2);
            contador = contador + 1;
        }

        jTableCursos.setModel(modelo);
    }

    private void cargarListaCarreras() throws SQLException, ExcepcionGlobal, NoDataException {
        ArrayList<Carrera> carreras = caData.getCarreras();
        for (Carrera carrera : carreras) {
            jComboCarreras.addItem(carrera);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGruModoBusqueda = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboCarreras = new javax.swing.JComboBox();
        jTbxNombre = new javax.swing.JTextField();
        jTbxSigla = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableCursos = new javax.swing.JTable();
        jBtnBuscar = new javax.swing.JButton();
        jRadioBCarrera = new javax.swing.JRadioButton();
        jRadioBSigla = new javax.swing.JRadioButton();
        jRadioBNombre = new javax.swing.JRadioButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Cursos Registrados");

        jLabel1.setText("Complete los terminos de búsqueda:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Sigla:");

        jLabel4.setText("Carrera:");

        jTbxNombre.setEnabled(false);

        jTbxSigla.setEnabled(false);

        jTableCursos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTableCursos);
        jTableCursos.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jBtnBuscar.setText("Buscar");
        jBtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBuscarActionPerformed(evt);
            }
        });

        jRadioBCarrera.setSelected(true);
        jRadioBCarrera.setText("Carrera");
        jRadioBCarrera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioBCarreraActionPerformed(evt);
            }
        });

        jRadioBSigla.setText("Sigla");
        jRadioBSigla.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jRadioBSiglaStateChanged(evt);
            }
        });
        jRadioBSigla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioBSiglaActionPerformed(evt);
            }
        });

        jRadioBNombre.setText("Nombre");
        jRadioBNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioBNombreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(3, 3, 3)
                            .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addGap(4, 4, 4)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTbxNombre)
                                .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(jBtnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioBCarrera)
                        .addGap(33, 33, 33)
                        .addComponent(jRadioBSigla)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioBNombre)))
                .addContainerGap(103, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioBCarrera)
                    .addComponent(jRadioBSigla)
                    .addComponent(jRadioBNombre))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(jBtnBuscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioBSiglaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioBSiglaActionPerformed
        jTbxSigla.enable(true);
        jTbxSigla.setText("");
        jTbxNombre.setText("--Inhabilitado--");
        jTbxNombre.enable(false);
        jComboCarreras.enable(false);
    }//GEN-LAST:event_jRadioBSiglaActionPerformed

    private void jRadioBCarreraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioBCarreraActionPerformed
        jTbxNombre.setText("--Inhabilitado--");
        jTbxSigla.setText("--Inhabilitado--");
        jTbxNombre.enable(false);
        jTbxSigla.enable(false);
        jComboCarreras.enable(true);
    }//GEN-LAST:event_jRadioBCarreraActionPerformed

    private void jRadioBSiglaStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jRadioBSiglaStateChanged
        // Este método por el momento no es necesario
    }//GEN-LAST:event_jRadioBSiglaStateChanged

    private void jBtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBuscarActionPerformed
        try {
            llenarTabla();
        } catch (SQLException ex) {
            Logger.getLogger(MostrarCurso.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(MostrarCurso.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(MostrarCurso.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }//GEN-LAST:event_jBtnBuscarActionPerformed

    private void jRadioBNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioBNombreActionPerformed
        jTbxSigla.setText("--Inhabilitado--");
        jTbxSigla.enable(false);
        jComboCarreras.enable(false);
        jTbxNombre.setText("");
        jTbxNombre.enable(true);
    }//GEN-LAST:event_jRadioBNombreActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGruModoBusqueda;
    private javax.swing.JButton jBtnBuscar;
    private javax.swing.JComboBox jComboCarreras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioBCarrera;
    private javax.swing.JRadioButton jRadioBNombre;
    private javax.swing.JRadioButton jRadioBSigla;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableCursos;
    private javax.swing.JTextField jTbxNombre;
    private javax.swing.JTextField jTbxSigla;
    // End of variables declaration//GEN-END:variables
}
